//
//  SearchResultCell.m
//  Cat
//
//  Created by 卢仕彤 on 2019/4/1.
//  Copyright © 2019 lushitong. All rights reserved.
//

#import "SearchResultCell.h"
#import "CouponView.h"
#import "BrokerageView.h"
#import "FLImageView.h"


@interface SearchResultCell()

@property (nonatomic, strong) UIView *operView;
@property (nonatomic, strong) FLImageView *shopImageView;
@property (nonatomic, strong) UILabel *shopInfoLabel;
@property (nonatomic, strong) CouponView *couponInfoView;
@property (nonatomic, strong) BrokerageView *brokerInfoView;
@property (nonatomic, strong) UILabel *nowMoneyLabel;
@property (nonatomic, strong) UILabel *yuanLabel;
@property (nonatomic, strong) UILabel *soleNumberLable;
@property (nonatomic, strong) FLImageView *storeImageView;
@property (nonatomic, strong) UILabel *storeLabel;

@end

@implementation SearchResultCell


- (void)setModel:(GroomsModel *)model {
    _model = model;
    CGFloat width = (ScreenWidth - kQKFitModuleDecimal(24 + 9)) / 2;
    [self.shopImageView setImageUrl:[NSURL URLWithString:model.mainImgUrl] size:CGSizeMake(width, width) cornerRadius:kQKFitModuleDecimal(0.0)];
    self.shopInfoLabel.text = model.title;
    self.couponInfoView.moneyNumber = [NSNumber numberWithInteger:[model.couponAmount integerValue]];
    self.brokerInfoView.commissionNumber = model.prizeAmount;
    self.nowMoneyLabel.text = [NSString stringWithFormat:@"￥%@",model.buyPrice];
    
    NSString *oldString = [NSString stringWithFormat:@"￥%@",model.marketAmount];
    NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:oldString];
    [attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)} range:NSMakeRange(0,oldString.length)];
    self.yuanLabel.attributedText = attributeMarket;
    self.soleNumberLable.text = [NSString stringWithFormat:@"已售%@",model.sellNum.stringValue];
    
    [self.couponInfoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo([CouponView getSizeWithMoneyNumber:[NSNumber numberWithInteger:[model.couponAmount integerValue]]]);
    }];
    
    [self.brokerInfoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo([BrokerageView getSizeWidthPrizeAmount:model.prizeAmount]);
    }];
    
    [self.storeImageView setImageUrl:[NSURL URLWithString:model.storeImg] size:CGSizeMake(kQKFitModuleDecimal(15.0), kQKFitModuleDecimal(15.0)) cornerRadius:kQKFitModuleDecimal(15.0 /2)];
    self.storeLabel.text = model.storeName;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self loadSubviews];
    }
    return self;
}

- (void)loadSubviews {
    self.contentView.layer.shadowColor = COLOR_WITH_HEX_A(0x000000, 0.2).CGColor;
    self.contentView.layer.shadowOffset = CGSizeZero;
    self.contentView.layer.shadowOpacity = 0.8; // 阴影透明度，默认0
    self.contentView.layer.shadowRadius = kQKFitModuleDecimal(3);    // 阴影半径，默认3
    self.contentView.layer.cornerRadius = kQKFitModuleDecimal(5);
    [self.contentView addSubviews:@[self.operView]];
    
    [self.operView addSubviews:@[self.shopImageView,
                                 self.shopInfoLabel,
                                 self.couponInfoView,
                                 self.brokerInfoView,
                                 self.nowMoneyLabel,
                                 self.yuanLabel,
                                 self.soleNumberLable,
                                 self.storeImageView,
                                 self.storeLabel]];
    
    [self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.and.right.equalTo(self);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-kQKFitModuleDecimal(8.0));
    }];
    
    [self.shopImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.and.right.equalTo(self);
        make.height.equalTo(@(kQKFitModuleDecimal(171)));
    }];
    
    [self.shopInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(8.0));
        make.right.equalTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(8.0));
        make.top.equalTo(self.shopImageView.mas_bottom).offset(8);
    }];
    
    [self.couponInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(48.0), kQKFitModuleDecimal(19)));
        make.left.equalTo(self.shopInfoLabel.mas_left);
        make.top.equalTo(self.shopInfoLabel.mas_bottom).offset(kQKFitModuleDecimal(3.0));
    }];
    
    [self.brokerInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.couponInfoView.mas_right).offset(kQKFitModuleDecimal(2.0));
        make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(74), kQKFitModuleDecimal(20)));
        make.centerY.equalTo(self.couponInfoView.mas_centerY);
    }];
    
    [self.nowMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopInfoLabel.mas_left);
        make.bottom.equalTo(self.operView.mas_bottom).offset(-kQKFitModuleDecimal(30.0));
    }];
    
    [self.yuanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nowMoneyLabel.mas_centerY);
        make.left.equalTo(self.nowMoneyLabel.mas_right).offset(kQKFitModuleDecimal(2.0));
    }];
    
    [self.soleNumberLable  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nowMoneyLabel.mas_centerY);
        make.right.equalTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(8.0));
    }];
    
    [self.storeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kQKFitModuleDecimal(15.0), kQKFitModuleDecimal(15.0)));
        make.left.equalTo(self.nowMoneyLabel.mas_left);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-kQKFitModuleDecimal(16.0));
    }];
    
    [self.storeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.storeImageView.mas_right).offset(kQKFitModuleDecimal(7.0));
        make.centerY.equalTo(self.storeImageView.mas_centerY);
        make.right.equalTo(self.contentView.mas_right).offset(-kQKFitModuleDecimal(7.0));
    }];
}

#pragma mark----- lazy init -----

- (UIView *)operView {
    if (!_operView) {
        _operView = [[UIView alloc] init];
        _operView.backgroundColor = [UIColor whiteColor];
        _operView.layer.cornerRadius = kQKFitModuleDecimal(6.0);
        _operView.layer.masksToBounds = YES;
    }
    return _operView;
}

- (FLImageView *)shopImageView {
    if (!_shopImageView) {
        _shopImageView = [[FLImageView alloc] init];
        _shopImageView.userInteractionEnabled = NO;
    }
    return _shopImageView;
}

- (UILabel *)shopInfoLabel {
    if (!_shopInfoLabel) {
        _shopInfoLabel = [UILabel labelWithFont:[UIFont boldSystemFontOfSize:kQKFitFont(12)] textColor:[UIColor blackColor]];
        _shopInfoLabel.numberOfLines = 2;
    }
    return _shopInfoLabel;
}

- (CouponView *)couponInfoView {
    if (!_couponInfoView) {
        _couponInfoView = [[CouponView alloc] init];
    }
    return _couponInfoView;
}

- (BrokerageView *)brokerInfoView {
    if (!_brokerInfoView) {
        _brokerInfoView = [[BrokerageView alloc] init];
        _brokerInfoView.layer.borderWidth = kQKFitModuleDecimal(1.0);
        _brokerInfoView.layer.borderColor = COLOR_WITH_HEX(0xF6B319).CGColor;
        _brokerInfoView.layer.masksToBounds = YES;
    }
    return _brokerInfoView;
}

- (UILabel *)nowMoneyLabel {
    if (!_nowMoneyLabel) {
        _nowMoneyLabel = [UILabel labelWithFont:[UIFont boldSystemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0xFF3F3F)];
    }
    return _nowMoneyLabel;
}

- (UILabel *)yuanLabel {
    if (!_yuanLabel) {
        _yuanLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0xB1B1B1)];
    }
    return _yuanLabel;
}

- (UILabel *)soleNumberLable {
    if (!_soleNumberLable) {
        _soleNumberLable = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xB1B1B1)];
    }
    return _soleNumberLable;
}

- (FLImageView *)storeImageView {
    if (!_storeImageView) {
        _storeImageView = [[FLImageView alloc] init];
        _storeImageView.layer.cornerRadius = kQKFitModuleDecimal(15)/ 2.0;
        _storeImageView.layer.masksToBounds = YES;
    }
    return _storeImageView;
}

- (UILabel *)storeLabel {
    if (!_storeLabel) {
        _storeLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0x333333)];
    }
    return _storeLabel;
}

@end
